﻿using System;
using System.Linq;
using com.kmer.db;

namespace com.kmer.ApiServer.Attributes {
	/// <summary>
	/// 表示必须登录
	/// </summary>
	[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
	public class MustLoginAttribute : Attribute {

	}
	/// <summary>
	/// 表示必须是管理员
	/// </summary>
	[AttributeUsage(AttributeTargets.Method| AttributeTargets.Class)]
	public class MustAdminAttribute : Attribute {
		/// <summary>
		/// 检查当前用户是否有权限
		/// IfMarriage == "0" 表示是系统管理员
		/// </summary>
		/// <returns></returns>
		public static bool Check(string UserID){
			using (var dc = new km_platformEntities()){
                var userId = Convert.ToInt16(UserID);
				return dc.sys_users.Any(item => 
					item.IsEffective && item.IsRemove==false
					&& item.UserID == userId
                    && item.UserType==1
				);
			}
		}
	}
	/// <summary>
	/// 必须是后台用户
	/// </summary>
	[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
	public class MustBackendAttribute : Attribute {
	}
	/// <summary>
	/// 必须是微信管理员
	/// </summary>
	[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
	public class MustWXAdminAttribute : Attribute {
	    /// <summary>
	    /// 检查当前用户是否有权限
	    /// IfMarriage == "0" 表示是系统管理员
	    /// </summary>
	    /// <returns></returns>
	    public static bool Check(string OpenId)
	    {
	        using (var dc = new km_platformEntities())
	        {
	            return dc.t_order_wx_account.Any(d => !d.IsDisable && !d.IsRemove && d.OpenId == OpenId &&
	                                                  d.AccountType == 0);
	        }
	    }
    }
}
